package com.example.novelweb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.novelweb.common.BaseResponse;
import com.example.novelweb.common.ErrorCode;
import com.example.novelweb.common.ResultUtils;
import com.example.novelweb.exception.BusinessException;
import com.example.novelweb.exception.ThrowUtils;
import com.example.novelweb.model.entity.Collect;
import com.example.novelweb.model.entity.Commentlike;
import com.example.novelweb.model.vo.UserVO;
import com.example.novelweb.service.CommentlikeService;
import com.example.novelweb.mapper.CommentlikeMapper;
import com.example.novelweb.service.RedisService;
import com.example.novelweb.utils.UserUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author PERSON
* @description 针对表【commentlike(用户评论点赞)】的数据库操作Service实现
* @createDate 2025-02-03 22:59:17
*/
@Service
public class CommentlikeServiceImpl extends ServiceImpl<CommentlikeMapper, Commentlike>
    implements CommentlikeService{


  @Resource
  private RedisService redisService;

  @Override
  public BaseResponse<List<Long>> getCommentIdList(Long novelId) {
    // 校验登录
    UserVO userVO = UserUtils.LoginCheck();
    if( userVO== null) {
      throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR,"用户未登录");
    }
    Long userId = userVO.getId();
    List<Long> commentIdList = getBaseMapper().listCommentIdByUserAndNovel(userId,novelId);

    return ResultUtils.success(commentIdList);
  }

  @Override
  public BaseResponse<Boolean> add(Long commentId,Long novelId) {
    // 校验已登录
    UserVO userVO = UserUtils.LoginCheck();
    if( userVO== null) {
      throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR,"用户未登录");
    }
    Long userId = userVO.getId();

    // 判断存在相关记录
    Commentlike one = query().eq("userId", userId).eq("commentId", commentId).one();
    // one == null
    // one != null && one.getStatus == 1
    // one != null && one.getStatus() == 0  => 忽略
    if(one ==null) {
      Commentlike like = new Commentlike();
      like.setUserId(userId);
      like.setNovelId(novelId);
      like.setCommentId(commentId);
      boolean result =  save(like);
      ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
    } else if(one.getStatus() == 1) {
      one.setStatus(0);
      boolean result = updateById(one);
      ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
    } else {
      return ResultUtils.success(true);
    }
    redisService.incrCommentLikeNum(commentId);

    return ResultUtils.success(true);

  }

  @Override
  public BaseResponse<Boolean> del(Long commentId) {
    // 校验已登录
    UserVO userVO = UserUtils.LoginCheck();
    if( userVO== null) {
      throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR,"用户未登录");
    }
    Long userId = userVO.getId();
    // 判断存在相关记录
    Commentlike one = query().eq("userId", userId).eq("commentId", commentId).one();
    // one == null                          => 忽略
    // one != null && one.getStatus() == 1  => 忽略
    // one != null && one.getStatus() == 0
    if(one != null && one.getStatus() == 0) {
      one.setStatus(1);
      boolean result = updateById(one);
      ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
      redisService.decrCommentLikeNum(commentId);
    }
    return ResultUtils.success(true);
  }

}




